package com.asktgapp.user.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asktgapp.JGTool.Utils;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.dialog.ConfirmSkipDialog;
import com.asktgapp.engineer.activity.AuthCommitSuccessActivity;
import com.asktgapp.modulebase.ServiceUtil;
import com.asktgapp.modulebase.common.util.Util;
import com.asktgapp.utils.ImageDisplayUtil;
import com.asktgapp.utils.SelectPhotoDialogFragment;
import com.jph.takephoto.model.TImage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/2/28
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class SubQulificationPicFragment extends BaseFragment {
    @InjectView(R.id.iv_pic1)
    ImageView mPic1;
    @InjectView(R.id.iv_pic2)
    ImageView mPic2;
    @InjectView(R.id.iv_pic3)
    ImageView mPic3;
    @InjectView(R.id.ll_tip)
    LinearLayout mTip;
    @InjectView(R.id.iv_tianjia)
    ImageView mAdd;
    @InjectView(R.id.tv_skip)
    TextView mSkip;
    @InjectView(R.id.fl2)
    FrameLayout mFl2;
    @InjectView(R.id.fl3)
    FrameLayout mFl3;
    @InjectView(R.id.tv_commit)
    TextView mCommit;
    private List<String> urls = new ArrayList<>();
    private Bundle bundle;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_sub_qualification, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        mAdd.setOnClickListener(this);
        mCommit.setOnClickListener(this);
        mSkip.setOnClickListener(this);
        mPic1.setOnClickListener(this);
        mPic2.setOnClickListener(this);
        mPic3.setOnClickListener(this);
        bundle = getActivity().getIntent().getBundleExtra("info");
    }

    @Override
    public void loadData() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_tianjia:
                if (urls.size() < 3) {
                    SelectPhotoDialogFragment selectPhotoDialogFragment = new SelectPhotoDialogFragment(new SelectPhotoDialogFragment.setResult() {
                        @Override
                        public void setResult(ArrayList<TImage> mImages) {
                            for (TImage vo : mImages) {
                                urls.add(vo.getPath());
                            }
                            if (urls.size() == 1) {
                                mFl2.setVisibility(View.GONE);
                                mFl3.setVisibility(View.GONE);
                                mSkip.setVisibility(View.GONE);
                                mTip.setVisibility(View.GONE);
                                ImageDisplayUtil.LoadDisImage(getActivity(), mPic1, urls.get(0));
                            } else if (urls.size() == 2) {
                                mFl2.setVisibility(View.VISIBLE);
                                mFl3.setVisibility(View.GONE);
                                mSkip.setVisibility(View.GONE);
                                mTip.setVisibility(View.GONE);
                                ImageDisplayUtil.LoadDisImage(getActivity(), mPic1, urls.get(0));
                                ImageDisplayUtil.LoadDisImage(getActivity(), mPic2, urls.get(1));
                            } else if (urls.size() == 3) {
                                mFl2.setVisibility(View.VISIBLE);
                                mFl3.setVisibility(View.VISIBLE);
                                mAdd.setVisibility(View.GONE);
                                mSkip.setVisibility(View.GONE);
                                mTip.setVisibility(View.GONE);
                                ImageDisplayUtil.LoadDisImage(getActivity(), mPic1, urls.get(0));
                                ImageDisplayUtil.LoadDisImage(getActivity(), mPic2, urls.get(1));
                                ImageDisplayUtil.LoadDisImage(getActivity(), mPic3, urls.get(2));
                            }
                        }
                    }, 3 - urls.size());
                    selectPhotoDialogFragment.show(getFragmentManager(), "selectPhotoDialogFragment");
                }
                break;
            case R.id.tv_skip:
                ConfirmSkipDialog confirmSkipDialog = new ConfirmSkipDialog(new ConfirmSkipDialog.onResultCallBack() {
                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onSure() {

                        requestNet();
                    }
                });
                confirmSkipDialog.show(getFragmentManager(), "ConfirmSkipDialog");
                break;
            case R.id.tv_commit:
                if (urls.size() == 0) {
                    showTost("请上传资质照片或者点击《跳过此步骤》", 0);
                    return;
                }
                requestNet();
                break;
            case R.id.iv_pic1:
                SelectPhotoDialogFragment selectPhotoDialogFragment1 = new SelectPhotoDialogFragment(new SelectPhotoDialogFragment.setResult() {
                    @Override
                    public void setResult(ArrayList<TImage> mImages) {
                        if (urls.size() > 0) {
                            urls.remove(0);
                        }
                        urls.add(0, mImages.get(0).getPath());
                        mTip.setVisibility(View.GONE);
                        ImageDisplayUtil.LoadDisImage(getActivity(), mPic1, urls.get(0));
                    }

                }, 1);
                selectPhotoDialogFragment1.show(getFragmentManager(), "selectPhotoDialogFragment");
                break;
            case R.id.iv_pic2:
                SelectPhotoDialogFragment selectPhotoDialogFragment2 = new SelectPhotoDialogFragment(new SelectPhotoDialogFragment.setResult() {
                    @Override
                    public void setResult(ArrayList<TImage> mImages) {
                        urls.remove(1);
                        urls.add(1, mImages.get(0).getPath());
                        ImageDisplayUtil.LoadDisImage(getActivity(), mPic2, urls.get(1));
                    }

                }, 1);
                selectPhotoDialogFragment2.show(getFragmentManager(), "selectPhotoDialogFragment");
                break;
            case R.id.iv_pic3:
                SelectPhotoDialogFragment selectPhotoDialogFragment3 = new SelectPhotoDialogFragment(new SelectPhotoDialogFragment.setResult() {
                    @Override
                    public void setResult(ArrayList<TImage> mImages) {
                        urls.remove(1);
                        urls.add(2, mImages.get(0).getPath());
                        ImageDisplayUtil.LoadDisImage(getActivity(), mPic3, urls.get(2));
                    }

                }, 1);
                selectPhotoDialogFragment3.show(getFragmentManager(), "selectPhotoDialogFragment");
                break;
        }
    }

    private void requestNet() {
        HashMap params = new HashMap();
        final Apiservice apiservice = ApiUtil.getInstance().create();
        ApiUtil.putDefultData(getActivity(), params);

        params.put("realname", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("realname")));
        params.put("gender", RequestBody.create(ServiceUtil.MULTIPART, bundle.getInt("gender") + ""));
        params.put("cardNo", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("cardNo")));
        params.put("price", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("price")));
        params.put("province", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("province")));
        params.put("city", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("city")));
        params.put("brand", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("brand")));
        params.put("onlineTime", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("onlineTime")));
        params.put("recommendReason", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("recommendReason")));
        params.put("trainExperience", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("trainExperience")));
        params.put("goodFeild", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("goodFeild")));
        params.put("description", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("description")));

        if (!Util.isEmpty(bundle.getString("company")))
            params.put("company", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("company")));
        else
            params.put("company", RequestBody.create(ServiceUtil.MULTIPART, "无"));

        if (!Util.isEmpty(bundle.getString("position")))
            params.put("position", RequestBody.create(ServiceUtil.MULTIPART, bundle.getString("position")));
        else
            params.put("position", RequestBody.create(ServiceUtil.MULTIPART, "无"));

        showProgress(getClass().getSimpleName());
        String headImgeP = bundle.getString("headImg");
        if (TextUtils.isEmpty(headImgeP))
            headImgeP = "";

        Call<ApiResponseBody> call = apiservice.subProjecter(params,
                ApiUtil.file2Part("cardPic1", bundle.getString("cardPic1"), ApiUtil.MULTIPART),
                ApiUtil.file2Part("cardPic2", bundle.getString("cardPic2"), ApiUtil.MULTIPART),
                ApiUtil.files2Parts("qualificationPic", urls.toArray(), ApiUtil.MULTIPART),
                ApiUtil.file2Part("headImg", headImgeP, ApiUtil.MULTIPART));

        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    startActivity(new Intent(getActivity(), AuthCommitSuccessActivity.class));
                    getActivity().finish();
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });
    }
}
